<%
' +----------------------------------------------------------------------
' | POPASP [ ASP MVC ]
' +----------------------------------------------------------------------
' | Copyright (c) 2016 http://popasp.com All rights reserved.
' +----------------------------------------------------------------------
' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
' +----------------------------------------------------------------------
' | Author: popasp <1737025626@qq.com>
' +----------------------------------------------------------------------



'获取/设置属性值，并非 Cookie,而是POP_MVC.config
Function C_(key)
	if isArray( key ) Then
		POP_MVC.config(key(0)) = key(1)
	elseif TypeName(key)="Dictionary" Then	'如果分配的是Dictionary对象则设置config
		set POP_MVC.config = POP_MVC.dict.Merge(POP_MVC.config,key)
	else
		C_ = POP_MVC.getConfig(key)
	End If
End Function

'读取/设置session
Function S_(key)
	dim i,rs,dict,k
	if isArray( key ) Then		'如果为数组，第一个元素是键名，第二个元素是值
		if isNull(key(1)) Then	'如果第二个元素为null,则删除该键值对
			call session.Contents.Remove( key(0) )
		elseif typename(key(1)) = "Recordset" Then
			set session( key(0) ) = POP_MVC.rs2dict( key(1) )
		elseif typename(key(1)) = "Dictionary" Then
			set session( key(0) ) = key(1)
		else
			if isObject( key(1) )  Then
				set session( key(0) ) = key(1)
			else 
				session( key(0) ) = key(1)
			End If
		End If		
	elseif typename(key) = "Recordset" Then
		set rs = key
		for i = 0 to rs.fields.count-1
			session( rs.fields(i).Name ) = rs.fields(i).Value
		next
	elseif typename(key) = "Dictionary" Then
		set dict = key
		for each k in dict
			if isNull( dict(k) ) then
				call session.Contents.Remove( k )
			else
				session( k ) = dict( k )
			end if
		next		
	elseif isNull(key) then
		session.Contents.RemoveAll	
	else
		if isObject( session(key) ) Then
			set S_ = session(key)
		else
			S_ = session(key)
		end If
	End If
End Function


'创建Dictionary对象
Function D_()
	Set D_ = POP_MVC.CreateDict()
End Function

' 实例化MVC的核心类
Function P_( ctrl )
	ctrl = UCase(ctrl)
	if left(ctrl,7) <> "POPASP_" Then
		ctrl = "POPASP_" & ctrl 
	End If
	Set P_ = POP_MVC.import( ctrl )
End Function

' 将json解析成Dictionary对象
Function js_decode( byval str )
	if len(str) > 0 then
		if AscB(MidB(str,1,1))=255 and AscB(MidB(str,2,1))=254 Then			
			str = MidB( str, 3 )
		End If
		
		set P_("JSON").data = D_()
		call P_("JSON").loadJSON(str)
		set js_decode = P_("JSON").data
	else
		js_decode = str
	end if
End Function

' 将json解析成Dictionary对象
Function js_encode( byref data )
	if isObject(data) then
		set P_("JSON").data = data
	else
		P_("JSON").data = data
	end if
	js_encode = P_("JSON").JSONoutput
End Function

' Strip HTML and ASP tags from a string 
Function strip_tags(byval fString) 
	Dim re 
	Set re = New RegExp 
	re.IgnoreCase = True 
	re.Global = True
	re.Pattern = "<(.[^>]*)>" 
	fString = re.Replace(fString, "") 
	Set re = Nothing 
	strip_tags = fString 
End Function 

' URL重定向
' 可只传入url,默认time=0，msg=""，如果想传入多个参数 array(url,time,msg)
' @param string url 重定向的URL地址
' @param integer time 重定向的等待时间（秒）
' @param string msg 重定向前的提示信息
' @return void
sub R_(args)
	on error resume next
    dim str,i,dict,url,time,msg
	set dict = D_	' 非POPASP下把D_换成 Server.CreateObject("Scripting.Dictionary")
	dict(0) = 0		'url
	dict(1) = 0		'time
	dict(2) = ""	'msg
	
	if isArray(args) Then
		for i = 0 to UBound(args)
			dict(i) = args(i)
		next
	else 
		dict(0) = args	'不是数组的话，默认当只传递了url
	end if
	
	url = dict(0) : time = dict(1) : msg = dict(2)
    if msg="" Then msg  = "系统将在" & time & "秒之后自动跳转到" & url & "！"	
	
	' 先当没有设置发送时redirect
	if 0 = time Then
		Response.Status = "302 Moved Temporarily"
		Response.Addheader "Location", url
	else
		Response.AddHeader "REFRESH", time & ";URL=" & url
		if -2147467259<>err.number Then
			Response.write msg
		End if
	end if
	' 如果出错，再当已经设置了发送头再进行redirect
    if -2147467259=err.number  then	'这个错误代码头信息已发送
		str    = "<meta http-equiv='Refresh' content='" & time& ";URL=" & url & "'>"
		if 0<>time then str =  str & msg
		Response.write str
	End If
	call POP_MVC.quit
End Sub

' 判断参数args是否为数组，或者为类数组的Dictionary对象
Function is_array( args )
	dim i,stype : stype = typename( args )
	dim key
	is_array = false
	if isArray(args) Then
		is_array = true
		exit function
	elseif stype = "Dictionary" Then
		i = 0
		for each key in args 
			if key <> i Then
				is_array = false
				exit Function
			End if
			i = i+1
		next
		is_array = true
	else 
		is_array = false
	End If		
End Function

'判断为空
Function is_empty(arg)	
	if isArray( arg ) Then
		if ubound(arg)<0 Then is_empty = true : exit Function
	end if
	if isNumeric(arg) Then
		if arg = 0 Then is_empty = true : exit Function
	End If
	is_empty = false
	select case typename(arg)
		case "Empty","Null","Nothing"
			is_empty = true
		case "Dictionary","IVariantDictionary","IRequestDictionary"
			is_empty = (0 = arg.count)
		case "Recordset"
			is_empty = (0 = arg.RecordCount)
		case "ISessionObject"
			is_empty = (0 = arg.Contents.Count)
		case "String"
			is_empty = ("" = arg)
		case "Boolean"
			is_empty = (false = arg)
		case else
			'其它情况还未发现 
	end Select
End Function

'判断是否为数字
Function is_numeric(arg)
	is_numeric = false
	if isNumeric(arg) Then
		if typename(arg) <> "Boolean" Then
			is_numeric = true
		End If
	end If
End Function

'判断是否本地运行
Function is_localhost()
is_localhost = (Request.ServerVariables("LOCAL_ADDR") = Request.ServerVariables("REMOTE_ADDR"))
End Function

' 记录和统计时间（微秒）和内存使用情况
' 使用方法:
' <code>
' G_("begin") ' 记录开始标记位
' ' ... 区间运行代码
' G_("end") ' 记录结束标签位
' Response.Write G_( array("begin","end",3) ) ' 统计区间运行时间 精确到小数后3位
' </code>
' @param string start 开始标签 args(0)
' @param string end 结束标签 args(1)
' @param integer dec 小数位 args(2)
' @return mixed
Function G_( args )
	if isArray(args) Then
		if isEmpty( POP_MVC.dG_( args(1) ) ) OR isEmpty( POP_MVC.dG_( args(0) ) ) Then
			G_ = "-"
		else
			G_ = POP_MVC.dG_( args(1) ) - POP_MVC.dG_( args(0) )
			
			if ubound(args) > 1 then
				G_ = FormatNumber(G_*1000,args(2),-1,0,0)
			else
				G_ = FormatNumber(G_*1000,0,-1,0,0)
			end if
		end if
	else
		POP_MVC.dG_( args ) = timer()		
	end if
End Function
%>